package com.photography.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.photography.entity.PageResult;
import com.photography.mapper.ProductMapper;
import com.photography.pojo.Product;
import com.photography.service.ProductService;
import com.photography.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
@Service
public class ProductServiceImpl implements ProductService {

    HashMap<String, Integer> map = new HashMap<>();

    @Autowired
    private ProductMapper productMapper;


    @Autowired
    private IdWorker idWorker;

    @Override
    public PageResult<Product> findAll(Product product,int page, int rows) {
        Page<Product> Page = new Page<>(page, rows);
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        wrapper.like(!StringUtils.isEmpty(product.getProductname()),"productname",product.getProductname()).eq("status",1).or().eq("status",2);
        Page<Product> productPage = productMapper.selectPage(Page, wrapper);
        PageResult<Product> productPageResult = new PageResult<>();
        productPageResult.setRows(productPage.getRecords());
        productPageResult.setTotal(productPage.getTotal());
        return productPageResult;
    }

    @Override
    public Product findById(String id) {
        Product product = productMapper.selectById(id);
        return product;
    }

    @Override
    public void addProduct(Product product) {
        product.setId(idWorker.nextId()+"");
        product.setCreated(new Date());
        product.setUpdated(product.getCreated());
        productMapper.insert(product);
    }


    @Override
    public void updateProduct(Product product) {
        productMapper.updateById(product);
    }

    @Override
    public void updateStatusProduct(String id) {
        Product product = new Product();
        product.setId(id);
        product.setStatus(3);
        productMapper.updateById(product);
    }

    @Override
    public List<Product> findAllList() {
        return productMapper.selectList(new QueryWrapper<Product>().eq("status","1").or().eq("status","0"));
    }

}
